package com.liupengyuan.framework.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.liupengyuan.framework.util.BeanMapUtil;
import com.liupengyuan.framework.util.ReflectGeneric;
import com.liupengyuan.framework.web.query.GenericDefaultPage;
import com.liupengyuan.framework.web.query.IGenericPage;

public abstract class MybatisBaseGenericDAOImpl<T> extends SqlSessionDaoSupport
		implements IBaseGenericDAO<T>{
	
	public MybatisBaseGenericDAOImpl() {
		sqlNamespace = ReflectGeneric.getClassGenricType(getClass(), 0).getName();
	}

	private String sqlNamespace;
	
	protected String getSqlNamespace(){
		return sqlNamespace;
	}
	/**
	 * 设置命名空间,并返回本身this
	 * @param sqlNamespace
	 * @return
	 */
	public MybatisBaseGenericDAOImpl<T> setSqlNamespace(String sqlNamespace) {
		this.sqlNamespace = sqlNamespace;
		return this;
	}
	
	protected String getSQLName(String sqlName){
		return getSqlNamespace() + "." + sqlName;
	}
	
	
	public void generateId(T ob) {
		System.out.println(getClass().getName()+".generateId");
	}
	
	/**
	 * 通过主键ID查询
	 */
	@SuppressWarnings("unchecked")
	public T getById(String id){
		return (T) getSqlSession().selectOne(getSQLName("getById"), id);
	}
	
	/**
	 * 添加一天记录,Java自动生成ID
	 * @ob 对象实体
	 */
	public Integer save(T ob){
		generateId(ob);
		return getSqlSession().insert(getSQLName("save"), BeanMapUtil.bean2Map(ob));
	}
	
	/**
	 * 更新一条记录
	 * @ob 对象实体,参数存在主键
	 */
	public Integer update(T ob){
		return getSqlSession().update(getSQLName("update"), ob);
	}

	/**
	 * 查询列表
	 * @param 查询参数
	 * @sort 排序字段
	 * @dir 排序的顺序 ASC DESC
	 */
	@SuppressWarnings("unchecked")
	public List<T> findListBy(T param, String sort, String dir){
		Map<String,String> paramMap = BeanMapUtil.bean2Map(param);
		if(sort != null&&(!"".equals(sort.trim()))){
			paramMap.put("sort", sort);
			
			if(dir == null||"".equals(dir.trim())){
				dir = "ASC";
			}
			paramMap.put("dir",dir);
		}
		
		return getSqlSession().selectList(getSQLName("findListBy"), paramMap);
	}
	/**
	 * 查询列表
	 * @param param
	 * @return
	 */
	public List<T> findListBy(T param){
		return findListBy(param,null,null);
	}
	/**
	 * 通过ID删除记录
	 * @serializable String id
	 */
	public Integer deleteById(Serializable serializable) {
		return  getSqlSession().delete(getSQLName("deleteById"), serializable);
	}
	/**
	 * 通过ID数组删除多条记录
	 * @serializable String[] ids
	 */
	public Integer deleteByIds(Serializable[] serializable) {
		return getSqlSession().delete(getSQLName("deleteByIds"), serializable);
	}

	/**
	 * 分页查询
	 * @param 查询参数
	 * @pageNo 页号
	 * @pageSize 每页的记录数
	 * @sort 排序字段
	 * @dir 排序的顺序 ASC DESC
	 */
	@SuppressWarnings({ "unchecked"})
	public IGenericPage<T> findPageBy(T param, int pageNo, int pageSize, String sort, String dir) {
		int count = getCountBy(param);
		if (count < 1)
			return GenericDefaultPage.emptyPage();
		Map<String, String> map = BeanMapUtil.bean2Map(param);
		if(StringUtils.isNotEmpty(sort)){
			map.put("sort", sort);
			
			if(StringUtils.isEmpty(dir)){
				dir = "ASC";
			}
			map.put("dir", dir);
		}
		int start = GenericDefaultPage.getStartOfPage(pageNo, pageSize) - 1;
		RowBounds rb = new RowBounds(start,pageSize);
		List<T> result = getSqlSession().selectList(getSQLName("findListBy"), map, rb);
		

		return new GenericDefaultPage(pageNo,pageSize, result,count);
	}

	/**
	 * 查询记录数
	 */
	public Integer getCountBy(T obj) {
		@SuppressWarnings("unchecked")
		Map<String, String> map = BeanMapUtil.bean2Map(obj);
		return (Integer) getSqlSession().selectOne(getSQLName("getCountBy"), map);
	}
	@SuppressWarnings("unchecked")
	public T selectOne (T obj){
		Map<String, String> map = BeanMapUtil.bean2Map(obj);
		return (T) getSqlSession().selectOne(getSQLName("login"), map);
	}
	

}
